package com.four.hospital.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.four.common.utils.R;
import com.four.hospital.dao.RecordDao;
import com.four.hospital.entity.Record;
import com.four.hospital.entity.User;
import com.four.hospital.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author:luojie
 * @Date: 2022/01/05/18:39
 * @Description
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordDao recordDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     *
     * 记录表根据患者id回显数据
     *
     * @param id
     * @return
     */
    @Override
    public Record findById(Long id) {
        return recordDao.findById(id);
    }

    /**
     * 支付
     * @param record
     * @param docId
     * @param userId
     */
    @Override
    public void pay(Record record, Integer docId, Integer userId) {
        record.setDocId(docId);
        record.setUserId(userId);
        record.setReDate(new Date());
        recordDao.pay(record);
    }

    /**
     * 诊断记录
     *
     * @param userToken
     * @return
     */
    @Override
    public R recordList(String userToken) {
        String s = redisTemplate.opsForValue().get(userToken);
        if (s==null) {
            return R.error(500,"您没有登录，请重新登录");
        }
        User user = JSON.parseObject(s, new TypeReference<User>() {
        });
        List<Record> records = recordDao.recordList(user.getUserId());
        return R.ok().put("data",records);
    }
}
